package com.blb.community_admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blb.community_admin.Annotation.Log;
import com.blb.community_admin.entity.SysDictType;
import com.blb.community_admin.entity.SysUser;
import com.blb.community_admin.entity.vo.UserVO;
import com.blb.community_admin.service.ISysUserService;
import com.blb.community_admin.util.EasyExcelUtil;
import com.blb.community_admin.util.JasyptEncryptorUtils;
import com.blb.community_admin.util.ResponseResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.http.ResponseEntity;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@Api
public class SysUserController {

    @Autowired
    private ISysUserService sysUserService;

    @GetMapping("/users")
    public ResponseResult<List<SysUser>> getSysUsers(){
        System.out.println();
        return ResponseResult.ok(sysUserService.list(null));
    }

    @Log(module = "用户管理", operationType = 2, requestMethod = "PUT")
    @PutMapping("/user")
    public ResponseResult<String> updateUser(@RequestBody UserVO user){
        System.out.println(user);
        return ResponseResult.ok("更新成功");
    }

    @ApiOperation("查询全部")
    @PostMapping("/users/text")
    public ResponseResult<IPage<SysUser>> getByText(Long current,@RequestBody SysUser sysUser) {
        System.out.println(sysUser);

        IPage<SysUser>  sysUser1= sysUserService.getAllUser(new Page<>(current, 3L),sysUser);
        return ResponseResult.ok(sysUser1);
    }

    @Log(module = "用户管理", operationType = 2, requestMethod = "PUT")
    @ApiOperation("修改状态")
    @PutMapping("/users/status")
    public ResponseResult<String> updateStadus(@RequestBody SysUser sysUser){
        System.out.println(sysUser);
        sysUserService.updateStatus(sysUser);
        return ResponseResult.ok("修改状态成功");
    }

    @Log(module = "用户管理", operationType = 1, requestMethod = "POST")
    @Transactional
    @ApiOperation("添加用户")
    @PostMapping("/users/adduser")
    public ResponseResult<String> addUser(String postId,@RequestBody SysUser sysUser){
        System.out.println(sysUser);
        sysUser.setCreateTime( LocalDateTime.now());

        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String encode = bCryptPasswordEncoder.encode(sysUser.getPassword());
        sysUser.setPassword(encode);
        SysUser sysuser1 = sysUserService.getOne(new QueryWrapper<SysUser>().lambda().eq(SysUser::getUserName, sysUser.getUserName()));

        if (sysuser1 == null){
            sysUserService.save(sysUser);
            SysUser sysuser = sysUserService.getByName(sysUser.getUserName());
            return ResponseResult.ok(""+sysuser.getUserId());
        }
            return ResponseResult.ok("添加失败");

    }

    @ApiOperation("根据userId查询")
    @GetMapping("/users/getById/{userId}")
    public ResponseResult<SysUser> getById(@PathVariable String userId){
        SysUser sysUser = sysUserService.getById(userId);
        return ResponseResult.ok(sysUser);
    }

    @ApiOperation("根据userId查询返回解密后的密码")
    @GetMapping("/users/getByIdPassword/{userId}")
    public ResponseResult<String> getByIdPassword(@PathVariable String userId){
        SysUser sysUser = sysUserService.getById(userId);


        return ResponseResult.ok(null);
    }


    @Log(module = "用户管理", operationType = 2, requestMethod = "PUT")
    @ApiOperation("根据id修改部分字段")
    @PutMapping("/users/updateuser")
    public ResponseResult<String> updateUser(@RequestBody SysUser sysUser){
        sysUser.setUpdateTime(LocalDateTime.now());
        System.out.println(sysUser);
        sysUserService.updateUserById(sysUser);
        return ResponseResult.ok("修改成功");
    }

    @Log(module = "用户管理", operationType = 3, requestMethod = "DELETE")
    @ApiOperation("删除")
    @DeleteMapping("/users/deleteuser/{userId}")
    public ResponseResult<String> deleteUser(@PathVariable Long userId){
        sysUserService.removeById(userId);
        return ResponseResult.ok("删除成功");
    }

    @Log(module = "用户管理", operationType = 5, requestMethod = "POST")
    @ApiOperation("导出")
    @PostMapping("/users/export")
    public ResponseEntity<ByteArrayResource> export(@RequestBody List<SysUser> sysUser) throws IOException {
        String path = EasyExcelUtil.write("用户管理", SysUser.class, sysUser);
        ResponseEntity<ByteArrayResource> download = EasyExcelUtil.download(path);
        return download;
    }

    @Log(module = "用户管理", operationType = 2, requestMethod = "PUT")
    @ApiOperation("重置密码")
    @PutMapping("/users/updatePassword")
    public ResponseResult<String> updatePassword(@RequestBody SysUser sysUser){

        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        String encode = bCryptPasswordEncoder.encode(sysUser.getPassword());
        sysUser.setPassword(encode);

        sysUserService.updatePassword(sysUser);

        return ResponseResult.ok("重置成功");
    }
}
